<?php



/** 
 * 控制器_买水
 * @property User_dev_mdl $userDev
 * @property User_order_mdl $order
 * @property Price_mdl $price
 * @property Order_queue_mdl $orderQueue
 * @property Dev_mdl $dev
 */
class Buy extends MY_Controller {
	
	public function __construct() {
		parent::__construct ();
		$this->user = parent::getLoginUser();
		$this->load->model('User_order_mdl','order');
		$this->load->model('User_dev_mdl','userDev');
		$this->load->model('Dev_mdl','dev');
		$this->load->model('Price_mdl','price');
// 		$this->load->model('Order_queue_mdl','orderQueue');
		
		$this->order->setUserID($this->user->id);
	}
	/**
	 * API:买水页数据
	 * 	余额及水价
	 */
	public function index(){
		$priceParams = $this->price->getParams($this->user->schoolID);
		$this->apiOutput->put('balance',(int) $this->user->balance)
						->put('priceParams',$priceParams)
						->out();
	}
	/**
	 * 授权
	 *  传入参数:
	 *  	devID:设备ID
	 */
	public function acess(){
		$this->checkBindPhone($this->user);
		
		$devID = (int)$this->input->post_get('devID');
		// 检查该设备是否可用
		$dev = $this->dev->getOneById($devID);
		if( empty($dev) || $dev['enable']==0 ){
			$this->apiOutput->error('该设备不可用',-21);
		}
				
		// 检查该用户是否有未完成订单
		if ($this->order->checkHasUnfinishOrder($this->user->id)) {
			$this->apiOutput->error ( '您的账号有未完成的订单,请稍后再试!', - 3 );
		} 
		
		$priceParams = $this->price->getParams($this->user->schoolID);
		$devType = $dev['type'];
		
		$this->apiOutput
				->put('balance', $this->user->balance)
				->put('devType', $devType)
				->put('priceParams',$priceParams)
				->out();
	}
	
	/**
	 * 开始取水
	 * 	传入参数:
	 * 		type : 1:热水,2:冷水,3:洗衣机模式,4、脉冲热水5、脉冲冷水
	 * 		devID : 设备ID
	 * 		lon : 经度
	 * 		lat : 纬度
	 */
	public function begin(){
		$type  = (int) $this->input->post_get('type');
		$devID = (int) $this->input->post_get('devID');
		$lon   = (string) $this->input->post_get('lon');
		$lat   = (string) $this->input->post_get('lat');
		
		$order = array(
				'userID'=>$this->user->id,
				'devID'=>$devID,
				'isFinish'=>0,
				'type'=>$type,
				'createTime'=>time(),
				'phoneID'=>$this->user->currentPhoneID,
				'lon'=>$lon,
				'lat'=>$lat,
				'mode'=>1,
				'schoolID'=>$this->user->schoolID,
		);
		if( $type===BUY_MODE_ONCE ){// 洗衣机模式
			$order['isFinish']=1;
			$order['updateTime'] = time();
			$order['takeSec'] = 300;
			$order['amount'] = $this->price->calculateAmount(0, $type, $this->user->schoolID);
		}
		
		$orderID = $this->order->insert($order);
		if( empty($orderID) ){
			$this->apiOutput->errorSystem();
		}
		
		if ( $type===BUY_MODE_ONCE  ) {
			// 向用户扣钱
			$this->users->buy($order['userID'], $order['amount']);
			// 添加到mongo同步队列
			$this->load->model('Order_sync_queue_mdl','syncQueue');
			$this->syncQueue->enqueue($orderID,$this->user->id);
			$this->apiOutput->put('amount', $order['amount'])
							->put('balance', $this->user->balance);
		}
		
		
		$this->apiOutput->put('orderID', $orderID)
						->out();
		
	}
	
	/**
	 * 结算订单
	 * 	传入参数:
	 * 		orderID : 订单ID
	 *      takeSec : 取水秒数
	 *      priceVer: 价格参数版本
	 */
	public function end(){
		$this->load->library('Validation','validation');
		if( ! $this->validation->checkSign() ){
			$this->apiOutput->errorInput('签名无效');
		}
		
		$orderID = (int) $this->input->post_get('orderID');
		$takeSec = (int) $this->input->post_get('takeSec');
		$rqst = (float) $this->input->post_get('rqst');
		$priceVer = (int) $this->input->post_get('priceVer');
		
		$amount = $this->order->end($orderID, $takeSec);
		
		$this->apiOutput->put('amount', $amount)
						->put('balance',$this->user->balance)
						->out();
	}
	
	
	public function offline(){
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
